//
//  SoundCollectionController.m
//  01_kakin
//
//  Created by jon due on 12/03/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SoundCollectionController.h"

@implementation SoundCollectionController

@synthesize soundTable = _soundTable;
@synthesize bgimgView = _bgimgView;
@synthesize backBtn = _backBtn;
@synthesize soundAry = _soundAry;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    isPlaying = NO;
    
    /*=== table ===*/
    self.soundTable.dataSource = self;
    self.soundTable.delegate = self;
    self.soundTable.backgroundColor = [ UIColor clearColor ];
    
    // 背景設定
    self.bgimgView.image = [ UIImage imageNamed:IMG_COL_BG ];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [ super viewWillAppear:animated ];
    
    self.soundAry = [[ NSArray alloc ]initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    _soundTable = nil;
    _bgimgView = nil;
    _backBtn = nil;
    
}

-(void)dealloc{
    [ _soundTable release ];
    [ _bgimgView release ];
    [ _backBtn release ];
    [ _soundAry release ];
    [ super dealloc ];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

/* ============================================================================== */
#pragma mark - button Action
/* ============================================================================== */

-(IBAction)backBtnAction:(UIButton*)sender{
    
    [ (SANavigationController*)self.navigationController SApopViewControllerAnimated:SAAnimationTypeDown ];
    
}

/* ============================================================================== */
#pragma mark - UItable Datasource
/* ============================================================================== */

// セクション数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// セクション内のセル数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ self.soundAry count ];
}

// 列の高さ
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

// セルの中身を返す

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
    CollectionCellView *cell = nil;
    
    static NSString *kAlertCell_ID = @"CollectionCellView";
    cell = [self.soundTable dequeueReusableCellWithIdentifier:kAlertCell_ID];
    if (cell == nil){
        
        cell = [[[ NSBundle mainBundle ] loadNibNamed:@"CollectionCellView" 
                                                owner:self
                                              options:nil]lastObject];
        
    }
    
    cell.messageLabel.text = [ NSString stringWithFormat:@"fineName NO%d",indexPath.row ];
    
    return cell;
        
}

/* ============================================================================== */
#pragma mark - UItable delegate
/* ============================================================================== */

// セルの選択時のアクション
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(isPlaying){
        return;
    }
    isPlaying =YES;
    
    soundBean *bean = [ self.soundAry objectAtIndex:indexPath.row ];
    
    SASoundPlayer *player = [[ SASoundPlayer alloc ]init];
    player.delegate = self;
    [ player play: bean.fileName ];
    
    [ player release];

}

/* ============================================================================== */
#pragma mark - SASoundDelegate delegate
/* ============================================================================== */

-(void)soundPlayer:(AVAudioPlayer*)player{
    
    // 再生が終了したらflgを下げる
    isPlaying = NO;
}


@end
